package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.CashDao;

@Repository("cashDao")
public class CashDaoImpl implements CashDao{

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public int getListCashCount(String userName, String userRole, String time) {
        
        String sql = "select count(*) from t_cash a "
                + " left join t_user u on u.user_id = a.user_id "
                + " where 1 = 1 ";
        
            if (StringUtil.isNotEmpty(userName) && !"undefined".equals(userName) ) {
                sql += " and u.user_name like '%"+userName+"%' ";
            }
            
            if (StringUtil.isNotEmpty(userRole) && !"undefined".equals(userRole) ) {
                sql += " and u.user_role = '"+userRole+"' ";
            }
            
            if (StringUtil.isNotEmpty(time) && !"undefined".equals(time) ) {
                sql += " and a.time = '"+time+"' ";
            }
            return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List getAllCash(String userName, String userRole, String time,
            int pageNo, int pageSize) {
        
        int beginRow = (pageNo - 1)* pageSize;
        
        String sql = "select a.account_balance,"
                + " a.cash_balance,"
                + " a.time,"
                + " a.card_number,"
                + " a.branch_account,"
                + " u.user_name,"
                + " u.user_role,"
                + " a.state,"
                + " a.type "
                + " from t_cash a "
                + " left join t_user u on u.user_id = a.user_id "
                + " where 1 = 1 ";
        
            if (StringUtil.isNotEmpty(userName) && !"undefined".equals(userName) ) {
                sql += " and u.user_name like '%"+userName+"%' ";
            }
            
            if (StringUtil.isNotEmpty(userRole) && !"undefined".equals(userRole) ) {
                sql += " and u.user_role = '"+userRole+"' ";
            }
            
            if (StringUtil.isNotEmpty(time) && !"undefined".equals(time) ) {
                sql += " and a.time = '"+time+"' ";
            }
            
            sql += " order by a.created_time DESC ";
            sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
            
            return jdbcTemplate.queryForList(sql);
    }

}
